<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>


	
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Pagebakers » jsonComponent for CakePHP</title>
		<meta name="generator" content="WordPress 2.1.2">
		<style type="text/css" media="screen">
			@import url( http://www.pagebakers.nl/wp-content/themes/pagebakersv2/style.css );
		</style>
		<script type="text/javascript" src="Pagebakers%20%C2%BB%20jsonComponent%20for%20CakePHP_files/pngfix.js"></script>	
		<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://www.pagebakers.nl/feed/">
		<link rel="alternate" type="application/atom+xml" title="Atom 0.3" href="http://www.pagebakers.nl/feed/atom/">
		<link rel="shortcut icon" type="image/ico" href="http://www.pagebakers.nl/wp-content/themes/pagebakersv2/favicon.ico">
		<link rel="pingback" href="http://www.pagebakers.nl/xmlrpc.php">
		<meta name="verify-v1" content="sdTcNJBUoRnl7yOJqBbgVukSfy6Sj/FdoGAEM85eakk=">
	    	<link rel="archives" title="January 2008" href="http://www.pagebakers.nl/2008/01/">
	<link rel="archives" title="December 2007" href="http://www.pagebakers.nl/2007/12/">
	<link rel="archives" title="November 2007" href="http://www.pagebakers.nl/2007/11/">
	<link rel="archives" title="September 2007" href="http://www.pagebakers.nl/2007/09/">
	<link rel="archives" title="August 2007" href="http://www.pagebakers.nl/2007/08/">
	<link rel="archives" title="June 2007" href="http://www.pagebakers.nl/2007/06/">
	<link rel="archives" title="May 2007" href="http://www.pagebakers.nl/2007/05/">
	<link rel="archives" title="April 2007" href="http://www.pagebakers.nl/2007/04/">
	<link rel="archives" title="March 2007" href="http://www.pagebakers.nl/2007/03/">
	<link rel="archives" title="February 2007" href="http://www.pagebakers.nl/2007/02/">
	<link rel="archives" title="January 2007" href="http://www.pagebakers.nl/2007/01/">
	<link rel="archives" title="December 2006" href="http://www.pagebakers.nl/2006/12/">
	<link rel="archives" title="November 2006" href="http://www.pagebakers.nl/2006/11/">
	<link rel="archives" title="October 2006" href="http://www.pagebakers.nl/2006/10/">
	<link rel="archives" title="September 2006" href="http://www.pagebakers.nl/2006/09/">
	<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://www.pagebakers.nl/xmlrpc.php?rsd">
	</head><body>
		<div id="container">
			<div id="header">
				<h1 id="logo"><a href="http://www.pagebakers.nl/">Pagebakers</a></h1>
				
				<ul id="mainMenu">
					<li>
						<a href="http://www.pagebakers.nl/">Home</a>
					</li>
					<li>
						<a href="http://www.pagebakers.nl/archives" class="active">Archive</a>
					</li>
					<li>
						<a href="http://www.pagebakers.nl/about">About</a>
					</li>
				</ul>
				

				<div id="search">
					<div id="search_area">
						<form id="searchform" method="get" action="/index.php">
							<div>
							<input class="searchfield" name="s" id="s" value="" title="Enter keyword to search" type="text">
							</div>
						</form>
					</div>
				</div>
			</div>
			
			<div id="content">
<!-- end header -->	<div id="blog">
		<div class="inner">
			  
			<div id="post-27" class="post alt">
				<h2><a href="http://www.pagebakers.nl/2007/01/04/jsoncomponent-for-cakephp/" rel="bookmark">jsonComponent for CakePHP</a></h2>	
				<span class="date">January 4th</span>

				<p class="tags"><img src="Pagebakers%20%C2%BB%20jsonComponent%20for%20CakePHP_files/tag.gif" alt=""><a href="http://www.pagebakers.nl/category/development/" title="View all posts in Development" rel="category tag">Development</a>,  <a href="http://www.pagebakers.nl/category/ajax/" title="View all posts in Ajax" rel="category tag">Ajax</a>,  <a href="http://www.pagebakers.nl/category/cakephp/" title="View all posts in CakePHP" rel="category tag">CakePHP</a>,  <a href="http://www.pagebakers.nl/category/javascript/" title="View all posts in Javascript" rel="category tag">Javascript</a>,  <a href="http://www.pagebakers.nl/category/json/" title="View all posts in JSON" rel="category tag">JSON</a></p>

				<div class="main">
					<p>Today I wrote my first CakePHP component, well actually I ported it from a PEAR package called <a href="http://pear.php.net/pepr/pepr-proposal-show.php?id=198">Service_JSON</a>.
Reason for this was that I needed a way to communicate between PHP and
Javascript, I allready used static JSON templates for some basic AJAX
action but I wanted a better and easier way to do this and I wanted to
be able to decode JSON to PHP.</p>
<p>Read more for the basic documentation how to use the component.<a id="more-27"></a><br>
[source:php]</p>
<p> // enable the component in your controller<br>
 var $components = array(’json’);</p>
<p> // convert a complex value to JSON notation, and send it to the view<br>
 $value = array(’foo’, ‘bar’, array(1, 2, ‘baz’), array(3, array(4)));<br>
 $output = $this-&gt;json-&gt;encode($value);</p>
<p> $this-&gt;set(’contents’, $output);<br>
 // prints: [”foo”,”bar”,[1,2,”baz”],[3,[4]]]</p>
<p> // accept incoming POST data, assumed to be in JSON notation<br>
 $input = file_get_contents(’http://yourjsonfile’, 1000000);<br>
 $value = $this-&gt;json-&gt;decode($input);</p>
<p> // It’s also possible to do some basic configuration from within the controller</p>
<p> $this-&gt;jsonLooseType; // Boolean<br>
 		// Enables loose typing.<br>
 		// “{…}” syntax creates associative arrays<br>
 		// instead of objects in decode().</p>
<p> $this-&gt;jsonSuppressErrors; // Boolean<br>
 		// Suppresses errors.<br>
		// Values which can’t be encoded (e.g. resources)<br>
		// appear as NULL instead of throwing errors.<br>
		// By default, a deeply-nested resource will<br>
		// bubble up with an error, so all return values<br>
		// from encode() should be checked with isError()</p>
<p>[/source]<br>
The component is licenced under <a href="http://www.opensource.org/licenses/bsd-license.php">BSD</a> and you can find the <a href="http://cakeforge.org/snippet/detail.php?type=snippet&amp;id=163">source code</a> on CakeForge</p>
<p><strong>Update</strong><br>
<a href="http://james.revillini.com/">James Revillini</a> made a small
change in the startup function of the controller as you can see in the
comments, thanks for that!. I updated the CakeForge with a new version
(0.1.1)</p>
				</div>
				
				<div class="meta">
					<ul>
						<li><img src="Pagebakers%20%C2%BB%20jsonComponent%20for%20CakePHP_files/link.png" alt="permalink icon"><a href="http://www.pagebakers.nl/2007/01/04/jsoncomponent-for-cakephp/">Permalink</a></li>
						<li><img src="Pagebakers%20%C2%BB%20jsonComponent%20for%20CakePHP_files/trackback.png" alt="permalink icon"><a href="http://www.pagebakers.nl/2007/01/04/jsoncomponent-for-cakephp/trackback/">Trackback</a></li>
						<li><img src="Pagebakers%20%C2%BB%20jsonComponent%20for%20CakePHP_files/comments.png" alt="permalink icon"></li>
					</ul>
				</div>
			</div>
			<!-- You can start editing here. -->
<div id="comments">
		<h3 class="reply">9 Responses to 'jsonComponent for CakePHP'</h3> 
	<p class="comment_meta">Comments <a href="http://www.pagebakers.nl/2007/01/04/jsoncomponent-for-cakephp/feed/"><abbr title="Really Simple Syndication">RSS</abbr></a> 
			or <a href="http://www.pagebakers.nl/2007/01/04/jsoncomponent-for-cakephp/trackback/" rel="trackback">TrackBack</a> to 'jsonComponent for CakePHP'.
		</p>
		<ol class="commentlist">

		
			<li class="alt" id="comment-3308">
				<h4 class="comment_author">
					<a href="http://james.revillini.com/" rel="external nofollow">james revillini</a>				</h4>
				
				<p class="metadate">February 24th</p>
				
				
				<div class="commentText"><p>This component did exactly what I was looking for!  It’s made it very easy to integrate with the Yahoo UI Datatable component.</p>
<p>I made a few changes to your code in the startup method so that
CakePHP would not show undefined variable notices. The new startup
method is:</p>
<p>	function startup(&amp;$controller) {<br>
		if(isset($controller-&gt;jsonLooseType) &amp;&amp; isset($controller-&gt;jsonSuppressErrors)) :<br>
			$this-&gt;use = SERVICES_JSON_LOOSE_TYPE | SERVICES_JSON_SUPPRESS_ERRORS;<br>
		elseif(isset($controller-&gt;jsonLooseType)) :<br>
			$this-&gt;use = SERVICES_JSON_LOOSE_TYPE;<br>
		elseif(isset($controller-&gt;jsonSuppressErrors)) :<br>
			$this-&gt;use = SERVICES_JSON_SUPPRESS_ERRORS;<br>
		else :<br>
			$this-&gt;use = 0;<br>
		endif;<br>
	}</p>
<p>I tried to enter this as a snippet in Cakeforge, but I couldn’t
figure out how to do it without submitting an entirely new snippet. I
was trying to make it a version 0.1.1 or something. If you know how to
do this, let me know and I’ll post it there.</p>
<p>On behalf of many web developers out there: thanks for your work.</p>
<p>Peace,<br>
James</p>
<p>p.s. the captcha is a bit too difficult to read most of the time.</p>
</div>

			</li>

		
		
			<li class="" id="comment-3332">
				<h4 class="comment_author">
					<a href="http://james.revillini.com/" rel="external nofollow">james revillini</a>				</h4>
				
				<p class="metadate">February 24th</p>
				
				
				<div class="commentText"><p>Two
things: First, that last code I posted is junk. Here’s a cleaner
version which will actually pay attention to the values of the
variables:<br>
	function startup(&amp;$controller) {</p>
<p>		//initialize variables if they have not been set yet<br>
		if (!isset($controller-&gt;jsonLooseType)) {<br>
			$controller-&gt;jsonLooseType = false;<br>
		}<br>
		if (!isset($controller-&gt;jsonSuppressErrors)) {<br>
			$controller-&gt;jsonSuppressErrors = false;<br>
		}</p>
<p>		if($controller-&gt;jsonLooseType &amp;&amp; $controller-&gt;jsonSuppressErrors) :<br>
			$this-&gt;use = SERVICES_JSON_LOOSE_TYPE | SERVICES_JSON_SUPPRESS_ERRORS;<br>
		elseif($controller-&gt;jsonLooseType) :<br>
			$this-&gt;use = SERVICES_JSON_LOOSE_TYPE;<br>
		elseif($controller-&gt;jsonSuppressErrors) :<br>
			$this-&gt;use = SERVICES_JSON_SUPPRESS_ERRORS;<br>
		else :<br>
			$this-&gt;use = 0;<br>
		endif;<br>
	}</p>
<p>Second, what is your preferred method of getting data from cake into
js variables? I don’t like the way I’m doing it now, which involves
putting script tags in my view like this:</p>
<p>	//html helper &amp; output data will not be available in external scripts<br>
	var author_add_url = ‘url(’/admin/authors/add’); ?&gt;’;<br>
	var data = ;</p>
<p>Thanks,<br>
James</p>
</div>

			</li>

		
		
			<li class="alt" id="comment-3517">
				<h4 class="comment_author">
					<a href="http://blog.pagebakers.nl/" rel="external nofollow">Eelco</a>				</h4>
				
				<p class="metadate">February 28th</p>
				
				
				<div class="commentText"><p>Great to see more people needed this !</p>
<p>I’ll check your changes when i’ve got time and I’ll update it on the CakeForge</p>
<p>My method for getting cake data into JS, isn’t that where json comes in?<br>
Here’s an example using AJAX<br>
<code><br>
The cake view<br>
{<br>
testurl : "/admin/test/blabla"<br>
}<br>
</code></p>
<p>Eval the responsetext<br>
<code><br>
var response = eval(request.responseText);<br>
alert(response.testurl);<br>
</code></p>
<p>Correct my if i’m wrong, it’s just a quick example :D</p>
<p>ps, </p>
<p>the captcha really sucks indeed, I should get another one</p>
</div>

			</li>

		
		
			<li class="" id="comment-3519">
				<h4 class="comment_author">
					<a href="http://www.pagebakers.nl/" rel="external nofollow">Eelco Wiersma</a>				</h4>
				
				<p class="metadate">February 28th</p>
				
				
				<div class="commentText"><p>Hmm, </p>
<p>What’s the matter with the CakeForge it really works like shit, I’m
also trying to add a new codefragment but without any succes.</p>
</div>

			</li>

		
		
			<li class="alt" id="comment-3981">
				<h4 class="comment_author">
					<a href="http://www.thetoine.net/" rel="external nofollow">Antoine</a>				</h4>
				
				<p class="metadate">March 23rd</p>
				
				
				<div class="commentText"><p>Awesome component, there’s small bug with the startup part, I changed to :</p>
<p>	function startup(&amp;$controller) {<br>
		//initialize variables if they have not been set yet<br>
		if (!isset($controller-&gt;jsonLooseType)) {<br>
			$controller-&gt;jsonLooseType = false;<br>
		}<br>
		if (!isset($controller-&gt;jsonSuppressErrors)) {<br>
			$controller-&gt;jsonSuppressErrors = false;<br>
		}</p>
<p>		if($controller-&gt;jsonLooseType &amp;&amp; $controller-&gt;jsonSuppressErrors) :<br>
			$this-&gt;use = SERVICES_JSON_LOOSE_TYPE | SERVICES_JSON_SUPPRESS_ERRORS;<br>
		elseif($controller-&gt;jsonLooseType) :<br>
			$this-&gt;use = SERVICES_JSON_LOOSE_TYPE;<br>
		elseif($controller-&gt;jsonSuppressErrors) :<br>
			$this-&gt;use = SERVICES_JSON_SUPPRESS_ERRORS;<br>
		else :<br>
			$this-&gt;use = 0;<br>
		endif;</p>
<p>		$this-&gt;controller =&amp; $controller;</p>
<p>		}</p>
<p>Thx for sharing.</p>
</div>

			</li>

		
		
			<li class="" id="comment-4296">
				<h4 class="comment_author">
					<a href="http://www.pablasso.com/2007/05/28/componente-de-json-en-cakephp" rel="external nofollow">pablasso.com » Blog Archive » Componente de JSON en CakePHP</a>				</h4>
				
				<p class="metadate">May 29th</p>
				
				
				<div class="commentText"><p>[…] componente muy útil desarrollado Eelco Wiersma en base al paquete de PEAR […]</p>
</div>

			</li>

		
		
			<li class="alt" id="comment-4297">
				<h4 class="comment_author">
					<a href="http://www.pagebakers.nl/2007/06/05/using-json-in-cakephp-12/" rel="external nofollow">Pagebakers » Using JSON in CakePHP 1.2</a>				</h4>
				
				<p class="metadate">June 5th</p>
				
				
				<div class="commentText"><p>[…] an half year ago I created jsonComponent for CakePHP, since then it has been used by quite a few people including […]</p>
</div>

			</li>

		
		
			<li class="" id="comment-4556">
				<h4 class="comment_author">
					<a href="http://muchasletras.blogspot.com/" rel="external nofollow">kchopein</a>				</h4>
				
				<p class="metadate">December 10th</p>
				
				
				<div class="commentText"><p>Hi!! </p>
<p>First of all, thanks for the work!</p>
<p>I’m trying to use this component with the Dojo Toolkit but I’m
having a problem: the generated json code includes the model level, and
I only need the data. I mean, I’m getting this:</p>
<p><code><br>
{"timestamp":1193692111, "items":[<br>
      {"Post":{"id":"1","title":"The title","body":"This is the post body.","created":"2007-11-07 00:44:54"}},<br>
      {"Post":{"id":"2","title":"A title once again","body":"And the post body follows.","created":"2007-11-07 00:45:15"}},<br>
      {"Post":{"id":"3","title":"Title strikes back","body":"This is really exciting! Not.","created":"2007-11-07 00:45:44"}},<br>
      {"Post":{"id":"4","title":"aaaaaa","body":"aaaaaadadasdas","created":"2007-11-12 10:00:45"}}<br>
]}<br>
</code></p>
<p>but I need something like this:</p>
<p><code><br>
{"timestamp":1193692111, "items":[<br>
      {"id":"1","title":"The title","body":"This is ...","created":"2007-11-07 00:44:54"},<br>
      {"id":"2","title":"A title once again","body":"And ...","created":"2007-11-07 00:45:15"}<br>
]}<br>
</code></p>
<p>Since I’m a Cake newbe I don’t know if there’s a way to do this or if I have to do it myself. How do you solve this?</p>
<p>Thanks in advance!!</p>
</div>

			</li>

		
		
			<li class="alt" id="comment-4558">
				<h4 class="comment_author">
					<a href="http://www.pagebakers.nl/" rel="external nofollow">Eelco Wiersma</a>				</h4>
				
				<p class="metadate">December 13th</p>
				
				
				<div class="commentText"><p>You
should manipulate the array CakePHP manually I think. I’m not sure what
server side code you’re using so I can’t give you any examples unless
you show me some code :)</p>
</div>

			</li>

		
		
		</ol>

	 	
	<h3 class="reply" id="respond">Leave a comment</h3>

	
		<form action="http://www.pagebakers.nl/wp-comments-post.php" method="post" id="commentform">
			<div class="postinput">
			
				<p><input name="author" id="author" value="" size="22" tabindex="1" type="text">
				<label for="author"><small>Name (required)</small></label></p>

				<p><input name="email" id="email" value="" size="22" tabindex="2" type="text">
				<label for="email"><small>Mail (will not be published) (required)</small></label></p>

				<p><input name="url" id="url" value="" size="22" tabindex="3" type="text">
				<label for="url"><small>Website</small></label></p>
			
			
				<p class="allowedtags"><small><strong>XHTML:</strong>
You can use the following tags: &lt;a href="" title=""&gt; &lt;abbr
title=""&gt; &lt;acronym title=""&gt; &lt;b&gt; &lt;blockquote
cite=""&gt; &lt;code&gt; &lt;em&gt; &lt;i&gt; &lt;strike&gt;
&lt;strong&gt; <br>Please use the &lt;code&gt; tag when posting code, thanks.</small></p>

				<p><textarea name="comment" id="comment" cols="100" rows="10" tabindex="4"></textarea></p>

				<p><input name="submit" id="submit" tabindex="5" value="Submit Comment" title="Check your comment before posting it" type="submit">
				<input name="comment_post_ID" value="27" type="hidden">
				</p>
							</div>
		</form>

	


	</div>

			
					
					</div>
	</div>


<!-- begin sidebar -->
<div id="sidebar">
				<div id="categories">
		<h3>Flavours</h3>
		<ul>
			<br>
<b>Fatal error</b>:  Call to undefined function dynamic_sidebar() in <b>/home/pagebake/public_html/wp-content/themes/pagebakersv2/sidebar.php</b> on line <b>22</b><br>
</ul></div></div></div></div></body></html>